<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-form ref="ruleForm" :rules="rules" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formInline.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formInline.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="onSubmit('ruleForm')">添加管理员</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="管理员用户名">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{scope.row.username}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.adminid)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAdminList, deleteAdmin, addAdmin } from '../../utils/api'
export default {
  data () {
    return {
      // 表格数据
      tableData: [],
      // 添加表单
      formInline: {
        username: '',
        password: ''
      },
      // 添加表单的校验
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  created () {
    // 获取管理员列表
    this.getAdminL();
  },
  methods: {
    // 添加管理员
    async onSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await addAdmin(this.formInline.username, this.formInline.password);
          if (res.status == 0) {
            this.$message({
              message: '添加管理员成功！',
              type: 'success'
            });
            this.getAdminL();
            this.formInline.username = '';
            this.formInline.password = '';
          } else {
            this.$message(res.message);
          }
        }
      });
    },
    // 删除管理员
    handleDelete (id) {
      this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await deleteAdmin(id)
        if (res.status == 0) {
          this.$message({
            message: '删除管理员成功！',
            type: 'success'
          });
          this.getAdminL();
        } else {
          this.$message.error(res.message);
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 获取管理员列表
    async getAdminL () {
      let res = await getAdminList();
      if (res.status == 0) {
        this.tableData = res.data;
      } else {
        this.$message.error('获取管理员列表失败！');
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>